<?php
// $Id$

/**
 * Controller_Admin_Columns 控制器
 */
class Controller_Admin_Columns extends Controller_Abstract
{

    function actionShowHide() {
        $ids = FUNC::getIdset(FUNC::getV('column_idset'));
        foreach($ids as $id) {
            $column = Column::find('col_id = ?', $id)->one()->query();
            if($column -> id()) {
                $column -> fields -> hidden = $this->_context->hidden;
                $column -> save();
            }
//            Column::meta()->destroyWhere('col_id = ?', $id);
        }
    }

    function actionIndex()
    {
        $condi = array();
        if($this->_context->parent_id) {
            $condi = array(
            'parent_id' => $this->_context->parent_id,
            );
        }
        else {
            $condi = array(
            'parent_id' => 0,
            );
        }

        $page = $this->_context->page;
        $pagesize = Q::ini('appini/background/column_list_pagesize');
        $columns = Column::find($condi)->limitPage($page, $pagesize)->order('created DESC')->query(array('parent'));
        $records = Column::find($condi)->all()->getCount('col_id');

        FUNC::getParent(Column::getFormCache(), $this->_context->parent_id, $parents, array('col_id'));    //查找父亲
        FUNC::getChildren(Column::getFormCache(null, $this->_context->parent_id), $childrens, array('col_id'));    //查找父亲
        //        Qdebug::dump($nodes);
        //        Qdebug::dump($childrens);
        //        Qdebug::dump(Column::getFormCache('aa', 283));
//         dump($this);
        $this->_view['records'] = $records;
        $this->_view['pagesize'] = $pagesize;
        $this->_view['columns'] = $columns;
        $this->_view['parents_array'] = $parents;
        $this->_view['childrens_array'] = $childrens;
//        $this->_fetchview();
    }

    function actionRemove() {
        //        $condi = array('role_id' => $this->_context->group_id);
        $ids = FUNC::getIdset(FUNC::getV('column_idset'));
        foreach($ids as $id) {
            Column::meta()->destroyWhere('col_id = ?', $id);
        }

        return $this->_redirectMessage('网站提示','数据已删除', $this->_context->referer());
    }

    function actionEdit() {
        //        echo $this->_context->baseDir().'../';
        //dump($this->_app);
        //dump(System::);

        FUNC::getParent(Column::getFormCache(),  $this->_context->col_id, $parents, array('col_id'));    //查找父亲
        //        unset($parents[0]);
        FUNC::formatArrayKey($parents);
          //    dump($parents);
        $condi = new QDB_Cond(
            'col_id = ?', $this->_context->col_id
        );
        $column = Column::find($condi)->one()->query();
        if(!$column->id()) {
            return $this->_redirectMessage('网站提示', '没有找到指定栏目');
        }

        $this->_view['system_droplist'] = System::droplist(array('name'=>'sys_id'), array($column->fields->sys_id));

        $this->_view['subject'] = '修改栏目';
        $this->_view['column'] = $column;
        $this->_view['parents_array'] = $parents;



    }

    function actionAdd() {
        $this->_view['system_droplist'] = System::droplist(array('name'=>'sys_id'));

        $this->_view['subject'] = '新增栏目';
    }

    function actionSave() {
        //        Qdebug::dump($_POST);
        $referer = $this->_context->referer() ? $this->_context->referer() : url('');
        $names = FUNC::getV('name');
        $parent = intval(FUNC::getV('parent'))<0?0:intval(FUNC::getV('parent'));
        $order = FUNC::getV('order');
        //        Qdebug::dump($names);
        foreach($names as $key => $name) {
            //            echo $name;
            //            echo '<br />'.$parent;
            $array = array(
                'name' => $name,
                'parent_id' => $parent,
            );
            $where[] = new QDB_Cond(
                'col_id = ?', $this->_context->col_id
            );
            $column = Column::find($array)->orWhere($where)->getOne();
            if($column -> id()) {
                if($parent !=0 && $column->col_id == $parent) {
                    //return $this->_redirectMessage('网站提示', '上级目录不能为本栏目', $referer);
                }
                else {
                    $column -> parent_id = $parent;

                }
            }
            else {
                $column -> parent_id = $parent;
            }
//                            Qdebug::dump($_POST);
            $column -> name = $name;
            $column -> sort = intval($order);
            $column -> fields -> hidden = intval(FUNC::getV('hidden'));
            $column -> fields -> sys_id = intval(FUNC::getV('sys_id'));
            $column -> fields -> outsite_url  = $this->_context->outsite_url;
            $column -> fields -> open_window  = $this->_context->open_window;
            
            $column -> save();
            
//            dump($column);
        }
        Column::clearCache();
        return $this->_redirectMessage('网站提示', '栏目已保存', url(''));
    }

    /**
     * 返回栏目JSON数据
     */
    function actionDumpParent()
    {
        $parent_id = intval($this->_context->parent_id);
        $result = Column::find('parent_id = ?', $parent_id)->all()->query()->toArray();
//$result = Column::getFormCache(null, $parent_id);
//dump($result);
        return json_encode($result);
    }

    function actionDumpChildren()
    {
        $col_id = intval($this->_context->col_id);
        $result = Column::find('col_id = ?', $col_id)->all()->query(array('fields'))->toArray();
        return json_encode($result);
    }

}